package com.gzist.elderlyserviceplatform.controller.admin;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gzist.elderlyserviceplatform.common.Result;
import com.gzist.elderlyserviceplatform.entity.dto.*;
import com.gzist.elderlyserviceplatform.entity.po.User;
import com.gzist.elderlyserviceplatform.service.UserService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;

/**
 * 用户表
 */
@RestController
@Tag(name = "用户管理")
@RequestMapping("/api/admin/user")
@RequiredArgsConstructor
public class UserControllerA {

    private final UserService userService;

    @SaIgnore
    @PostMapping("/login")
    @Transactional(rollbackFor = Exception.class)
    @Operation(summary = "用户登录", description = "User")
    public Result<SaTokenInfo> login(
            @RequestBody
            @Parameter(description = "登录参数") LoginDTO loginDTO
    ) {
        userService.login(loginDTO);
        return Result.success(StpUtil.getTokenInfo(), "登录成功");
    }

    @PostMapping("/list")
    @Operation(summary = "用户列表获取", description = "分页过滤获取用户列表")
    public Result<Page<User>> page(
            @RequestBody
            @Parameter(description = "接口参数") UserPageDTO userPageDTO
    ) {
        return Result.success(userService.queryPage(userPageDTO), "查询成功!");
    }

    @GetMapping("/detail/{id}")
    @Operation(summary = "用户查看详情", description = "User")
    public Result<User> getUserById(
            @PathVariable("id")
            @Parameter(description = "用户id") String id
    ) {
        return Result.success(userService.getById(id), "查询成功!");
    }

    @PostMapping("/add")
    @Operation(summary = "新增用户", description = "方便后端添加用户测试(无需对接,页面没有该功能)")
    public Result<Void> addUser(
            @RequestBody
            @Parameter(description = "用户信息") User user
    ) {
        userService.save(user);
        return Result.success("操作成功!");
    }

    @PostMapping("/update")
    @Operation(summary = "编辑用户", description = "编辑用户信息")
    public Result<Void> update(
            @RequestBody
            @Parameter(description = "用户信息") UserInfoSave0rUpdateDTO user
    ) {
        userService.updateUser(user);
        return Result.success("操作成功!");
    }

    @SaIgnore
    @PostMapping("/register")
    @Operation(summary = "用户注册", description = "User")
    public Result<Void> registerUser(
            @RequestBody
            @Parameter(description = "注册参数") RegisterUserDto registerUserDto
    ) {
        userService.register(registerUserDto);
        return Result.success("注册成功!");
    }

    @GetMapping("/loginerInfo")
    @Operation(summary = "获取当前登录用户信息", description = "获取当前登录用户信息")
    public Result<User> getLoginerInfo() {
        User user = userService.getById(StpUtil.getLoginIdAsString());
        return Result.success(user, "查询成功!");
    }

    @PostMapping("/logout")
    @Operation(summary = "用户登出", description = "User")
    public Result<Void> logout() {
        StpUtil.logout(StpUtil.getLoginIdAsString());
        return Result.success("登出成功!");
    }

    @PostMapping("/delete/{id}")
    @Operation(summary = "删除用户", description = "User")
    public Result<Void> delete(
            @PathVariable("id")
            @Parameter(description = "用户id") String id
    ) {
        userService.removeById(id);
        return Result.success("删除成功!");
    }

    @PostMapping("/batchDelete")
    @Operation(summary = "批量删除用户", description = "User")
    public Result<Void> batchDelete(
            @RequestBody
            @Parameter(description = "用户id集") String[] ids
    ) {
        userService.removeByIds(Arrays.asList(ids));
        return Result.success("删除成功!");
    }

    @SaIgnore
    @PostMapping("/verifyAccount")
    @Operation(summary = "验证账号是否存在", description = "用于在重置密码的校验")
    public Result<String> verifyAccount(
            @RequestBody
            @Parameter(description = "接口参数:{username,verifyToken,newPassword}") Map<String, String> param
    ) {
        return Result.success(userService.verifyAccount(param), "验证成功");
    }

    @SaIgnore
    @PostMapping("/resetPassword")
    @Operation(summary = "重置密码", description = "User")
    public Result<Void> submitReset(
            @RequestBody
            @Parameter(description = "接口参数:{username,verifyToken,newPassword}") Map<String, String> param
    ) {
        userService.resetPassword(param);
        return Result.success("重置成功");
    }

    @PostMapping("/updatePassword")
    @Operation(summary = "修改管理员密码", description = "需要验证原密码")
    public Result<Void> updatePassword(
            @RequestBody
            @Parameter(description = "接口参数") UpdatePasswordDTO updatePasswordDTO
    ) {
        userService.updatePassword(updatePasswordDTO);
        return Result.success("密码修改成功");
    }
}